package com.csf.controller.interceptor;

import com.csf.entity.SessionReplace;
import com.csf.service.DateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class DateInterceptor implements HandlerInterceptor {

   @Autowired
   private DateService dateService;

   @Autowired
   private SessionReplace sessionReplace;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        首先获取访问的应用程序
        String ip = request.getRemoteHost();
        dateService.saveUV(ip);
//        然后获取当前登录的用户
        if (sessionReplace.getUser() !=null) {
            int id = sessionReplace.getUser().getId();
            dateService.saveDAU(id);
        }
        return true;
    }
}
